package com.zou.service.impl;

import com.zou.commons.constants.Constants;
import com.zou.commons.domain.ReturnObject;
import com.zou.mapper.SysUserMapper;
import com.zou.pojo.SysUser;
import com.zou.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("sysUserService")
@Transactional
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public List<SysUser> querySomeSysUserBySysUser(SysUser sysUser) {
        return sysUserMapper.selectSomeSysUserBySysUser(sysUser);
    }

    @Override
    public ReturnObject sysUserLogin(String username, String password) {
        ReturnObject returnObject = new ReturnObject();
        //封装参数
        Map<String, Object> map = new HashMap<>();
        map.put("name", username);
        map.put("pwd", password);

        SysUser sysUser = sysUserMapper.selectSysUserByNameAndPwd(map);

        if (sysUser == null) {
            //登录失败,用户名或者密码错误
            returnObject.setFail("用户名或者密码错误");
        } else {//进一步判断账号是否合法
            if (sysUser.getState().equals(Constants.STATE_DISABLED)) {
                //登录失败，状态被锁定
                returnObject.setFail("状态被锁定");
            } else {
                //登录成功
                returnObject.setSuccess();
                returnObject.setOther(sysUser);
            }
        }
        return returnObject;
    }

    @Override
    public ReturnObject addSysUser(SysUser sysUser) {
        ReturnObject returnObject = new ReturnObject();
        try {
            if (sysUserMapper.insertSysUser(sysUser) > 0) {
                returnObject.setSuccess();
            } else {
                returnObject.setFail("系统忙，请稍后重试");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setFail("系统忙，请稍后重试");
        }
        return returnObject;
    }

    @Override
    public ReturnObject delSysUserById(Integer id) {
        ReturnObject returnObject = new ReturnObject();
        try {
            if (sysUserMapper.deleteSysUserById(id) > 0) {
                returnObject.setSuccess();
            } else {
                returnObject.setFail("系统忙，请稍后重试");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setFail("系统忙，请稍后重试");
        }
        return returnObject;
    }

    @Override
    public SysUser querySysUserById(Integer id) {
        SysUser sysUser = null;
        try {
            sysUser = sysUserMapper.selectSomeSysUserBySysUser(new SysUser().setUid(id)).get(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sysUser;
    }

    @Override
    public ReturnObject editSysUser(SysUser sysUser) {
        ReturnObject returnObject = new ReturnObject();
        try {
            if (sysUserMapper.updateSysUser(sysUser) > 0) {
                returnObject.setSuccess();
            } else {
                returnObject.setFail("系统忙，请稍后重试");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setFail("系统忙，请稍后重试");
        }
        return returnObject;
    }

    @Override
    public ReturnObject editSysUserStateById(String state, String id) {
        ReturnObject returnObject = new ReturnObject();
        //封装参数
        Map<String, Object> map = new HashMap<>();
        map.put("state", state);
        map.put("id", id);

        try {
            if (sysUserMapper.updateSysUserStateById(map) > 0) {
                returnObject.setSuccess();
            } else {
                returnObject.setFail("系统忙，请稍后重试");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setFail("系统忙，请稍后重试");
        }
        return returnObject;
    }

}
